/*
 *    Copyright (c) 2018-2025, daoism All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: daoism
 */
package com.mkh.st.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkh.st.admin.api.dto.SysBusinessSetDTO;
import com.mkh.st.admin.api.entity.SysBusinessSet;
import com.mkh.st.admin.api.vo.SysBusinessSetVO;
import com.mkh.st.admin.mapper.SysBusinessSetMapper;
import com.mkh.st.admin.service.SysBusinessSetService;
import com.mkh.st.common.core.constant.CommonConstants;
import com.mkh.st.common.core.util.R;
import com.mkh.st.common.job.client.JobManager;
import com.mkh.st.common.security.util.SecurityUtils;
import com.mkh.st.warehouse.api.feign.IWarehouseProvider;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 业务配置表
 *
 * @author daoism code generator
 * @date 2021-09-22 15:33:14
 */
@Service
@AllArgsConstructor
public class SysBusinessSetServiceImpl extends ServiceImpl<SysBusinessSetMapper, SysBusinessSet> implements SysBusinessSetService {

 	private final SysBusinessSetMapper businessSetMapper;

	private final IWarehouseProvider warehouseProvider;

	@Override
	public Boolean businessSetSave(SysBusinessSetDTO dto) {
		SysBusinessSet sysBusinessSet= BeanUtil.toBean(dto,SysBusinessSet.class);
		if(ObjectUtil.isNotNull(dto.getId())){
			SysBusinessSet exist = businessSetMapper.selectById(dto.getId());
			if(ObjectUtil.isNotNull(exist.getJobId())){
				JobManager.updateTimeJob(exist.getJobId(),dto.getPurchaseTime(),dto.getSiteId().toString());
				if(dto.getPurchaseCreateFlag()){
					JobManager.startJob(exist.getJobId());
				}else {
					JobManager.stopJob(exist.getJobId());
				}
			}else {
				if(dto.getPurchaseCreateFlag()){
					Integer jobId = JobManager.addTimeJob("generatePurchaseDoc",dto.getPurchaseTime(),dto.getSiteId().toString());
					JobManager.startJob(jobId);
					sysBusinessSet.setJobId(jobId);
				}
			}
			businessSetMapper.updateById(sysBusinessSet);
		}else {
			sysBusinessSet.setTenantId(SecurityUtils.getUser().getTenantId());
			sysBusinessSet.setDelFlag(CommonConstants.ZERO);
			if(dto.getPurchaseCreateFlag()){
				Integer jobId = JobManager.addTimeJob("generatePurchaseDoc",dto.getPurchaseTime(),dto.getSiteId().toString());
				JobManager.startJob(jobId);
				sysBusinessSet.setJobId(jobId);
			}
			businessSetMapper.insert(sysBusinessSet);
		}
		return Boolean.TRUE;
	}

	@Override
	public SysBusinessSetVO getBusnessSet(Long siteId) {
		SysBusinessSet sysBusinessSet = businessSetMapper.selectOne(Wrappers.<SysBusinessSet>lambdaQuery()
			.eq(SysBusinessSet::getSiteId,siteId).eq(SysBusinessSet::getDelFlag,CommonConstants.ZERO));
		if(ObjectUtil.isNull(sysBusinessSet)){
			return null;
		}
		SysBusinessSetVO vo=BeanUtil.toBean(sysBusinessSet,SysBusinessSetVO.class);
		if(ObjectUtil.isNotNull(sysBusinessSet.getDefaultWarehouse())){
			R<String> data = warehouseProvider.getWarehouseName(sysBusinessSet.getDefaultWarehouse());
			vo.setWarehouseName(data.getData());
		}
		return vo;
	}
}
